<template>
    <div id="content">
        <div class="page-header">
            <div class="container-fluid">
                <div class="pull-right">
                    <a form="form-customer-group"
                            data-toggle="tooltip" title=""
                            v-on:click="do_edit()"
                            class="btn btn-primary" data-original-title="保存">
                        <i class="fa fa-save"></i>
                    </a>
                    <a href="#" data-toggle="tooltip" title=""
                       class="btn btn-default" data-original-title="取消">
                        <i class="fa fa-reply"></i>
                    </a>
                </div>
                <h1>客户群组</h1>
                <ul class="breadcrumb">
                    <li><a href="#">首页</a></li>
                    <li><a href="#">客户群组</a></li>
                </ul>
            </div>
        </div>
        <div class="container-fluid">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="fa fa-pencil"></i> 编辑客户群组</h3>
                </div>
                <div class="panel-body">
                    <form action="#" method="post" enctype="multipart/form-data" id="form-customer-group"
                          class="form-horizontal">
                        <div class="form-group required">
                            <label class="col-sm-2 control-label">客户群组名称</label>
                            <div class="col-sm-10 ">

                                    <input type="text" v-model="customer_group.name"
                                           placeholder="客户群组名称"
                                           id="input-fullname" class="form-control">

                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-description2">描述</label>
                            <div class="col-sm-10">
                                <textarea v-model="customer_group.description" rows="5"
                                          placeholder="描述" id="input-description2"
                                          class="form-control"></textarea>
                            </div>
                        </div>

                        <div class="form-group hidden">
                            <label class="col-sm-2 control-label"><span data-toggle="tooltip" title="" >默认地址</span></label>
                            <div class="col-sm-10">
                                <label class="radio-inline">
                                    <input type="radio" v-model="customer_group.is_default"  value="1" >
                                    是
                                </label>
                                <label class="radio-inline">
                                    <input type="radio" v-model="customer_group.is_default"  value="0" >
                                    否
                                </label>
                            </div>
                        </div>


                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-sort-order">排序</label>
                            <div class="col-sm-10">
                                <input type="text" v-model="customer_group.sort_order"
                                       placeholder="排序" id="input-sort-order"
                                       class="form-control">
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data: function () {

            return {

                customer_group_id: 0,
                customer_group:{
                    name:'',
                    description:'',
                    sort_order:'',
                    is_default:0
                }
            }
        },

        methods: {

            get_group_info: function () {

                var url = this.config.web_url + '/admin.php?r=customer/customer_group/info';
                url += '&customer_group_id=' + this.customer_group_id;
                var t = this;

                this.axios.get(url).then(function (res) {

                    t.customer_group = res.data.customer_group;

                });

            },

            do_edit:function(){

                var url = this.config.web_url + '/admin.php?r=customer/customer_group/edit';
                url += '&customer_group_id=' + this.customer_group_id;
                var t = this;

                this.customer_group.name = this.customer_group.name.replace(/^(\s+)|(\s+)$/,'');
                if( !this.customer_group.name ){
                    alert( '名称不能为空' );
                    return;
                }
                var data = "name=" + this.customer_group.name;
                data += "&description=" + this.customer_group.description;
                // data += "&is_default=" + this.customer_group.is_default;
                data += "&sort_order=" + this.customer_group.sort_order;
                this.axios.post( url, data ).then(function (res) {

                    if( !res.data.status ){
                        t.$router.push('/customer/customer_group');
                    }

                });

            }
        },

        created: function () {

            if (this.$route.params && this.$route.params.customer_group_id ) {
                this.customer_group_id = this.$route.params.customer_group_id;
                this.get_group_info();
            }

        }

    }
</script>

<style scoped>


</style>